PFont font;
int speed=10;
float sp=15.0;
int count=1;
int coord=1;
int x=200;
int y=110;
int block_x, block_y,i;
int[][] snake;

void setup() {
  size(400,450);
  frameRate(sp);
  fill(100,50,50);
  i=1;
  snake = new int[100][3];
  for (i=0;i<=99;i++) {
    snake[i][1] = 600;
    snake[i][2] = 600;
  }
  snake[1][1] = x;
  snake[1][2] = y;
  block_x=int(random(2,36))*10;
  block_y=int(random(7,42))*10;
}

void draw() {
  background(10,10,10);
  fill(100,55,55);
  noStroke();
  rect(0,50,400,20);
  rect(0,50,20,400);
  rect(0,430,400,20);
  rect(380,50,20,400);
  fill(0,0,0);
  rect(0,0,400,50);
  font = loadFont("AppleCasual-20.vlw");
  textFont(font);
  fill(0, 102, 153);
  text("Score:", 50, 30); 
  text("Speed:", 250,30);
  text(sp, 315,30);
  text(count, 110,30);
  stroke(153);
  if (count>1) {
    for (i=count;i>1;i--) {
      snake[i][1]=snake[i-1][1];
      snake[i][2]=snake[i-1][2];
    }
  }
  rect(block_x,block_y,9,9);
  if (((snake[1][1]+5>block_x) && (snake[1][1]+5<block_x+10))&&((snake[1][2]+5>block_y) && (snake[1][2]+5<block_y+10))) {
    count++;
    block_x=int(random(2,36))*10;
    block_y=int(random(7,42))*10;
    sp=sp+0.5;
  }
  if ((snake[1][1]<20) || (snake[1][1]>370) || (snake[1][2]<70) || (snake[1][2]>420)) {
     javax.swing.JOptionPane.showMessageDialog(null, "Игра окончена \n Ваши съели: " + count + " чего-то))))");
     exit();
  }
 
  if (coord == 1) {
    snake[1][2]=snake[1][2]+speed;
  }
  if (coord == 2) {
    snake[1][2]=snake[1][2]-speed;
  }
  if (coord == 3) {
    snake[1][1]=snake[1][1]-speed;
  }
  if (coord == 4) {
    snake[1][1]=snake[1][1]+speed;
  }
   for (i=2;i<=count;i++) {
    if (snake[1][1] == snake[i][1] && snake[1][2] == snake[i][2]) {
       javax.swing.JOptionPane.showMessageDialog(null, "Игра окончена \n Ваши съели: " + count + " чего-то))))");
       exit();
    }
  }
    for (i=1;i<=count;i++) {
    rect(snake[i][1],snake[i][2],9,9);
  }
  
}
void keyPressed()
{
  if (key == CODED) {
    if((keyCode == UP) && coord != 1) {
      coord=2;
    } 
    if(keyCode == DOWN && coord != 2) {
      coord=1;
    } 
    if(keyCode == LEFT && coord != 4) {
      coord=3;
    } 
    if(keyCode == RIGHT && coord != 3) {
      coord=4;
    }  
  }
}
